﻿using clf.weighing.entity;
using MyHelper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace weighing
{
    public class AsyncFactory
    {
        #region Instance

        private static AsyncFactory? _instance;
        public static AsyncFactory Instance
        {
            get
            {
                _instance ??= new AsyncFactory();
                return _instance;
            }
        }

        private AsyncFactory() { }


        #endregion

        /// <summary>
        /// 异步保存车辆
        /// </summary>
        /// <param name="plate">车牌号</param>
        /// <returns>-1 exist; 0 failed ;1 success</returns>
        public async Task<int> SaveCarAsync(string plate, string driver = "", string phone = "")
        {
            Debug.WriteLine($"SaveCarAsync:{plate}");
            if (string.IsNullOrEmpty(plate) || !RegexHelper.IsVehicleNumber(plate)) return 0;
            
            try
            {
                var res = await Task<int>.Factory.StartNew(() =>
                {

                    bool exist = CarDao.Instance.IsExist(plate);
                    if (exist) return -1;

                    var car = new CarInfo
                    {
                        carNumber = plate,
                        isDelete = 0,
                        id = IdHelper.LongID,
                        addUserId = App.CurrentUser?.id ?? 0,
                        addUserName = App.CurrentUser?.name ?? "",
                        updateTime = DateTime.Now,
                        driver = driver,
                        driverIdnumber = "0",
                        driverMobile = phone,
                    };
                    car.addTime = car.updateTime;
                    car.updateUserId = (long)car.addUserId;
                    car.updateUserName = car.addUserName;
                    car.traeWeight = new decimal(0.0);
                    var res = CarDao.Instance.Sqlite.Insert(car);
                    return res;
                });

                return res;
            }
            catch (Exception ex)
            {
                var msg = "异步保存车辆出错：" + ex.Message;
                Log.Error(msg);
            }
            return 0;
        }

        /// <summary>
        /// 异步保存公司
        /// </summary>
        /// <param name="companyName">公司名称</param>
        /// <returns>-1 exist; 0 failed ;1 success</returns>
        public async Task<int> SaveCompanyAsync(string companyName)
        {
            Debug.WriteLine($"SaveCompanyAsync:{companyName}");
            if (string.IsNullOrEmpty(companyName)) return 0;
            try
            {
                var res = await Task<int>.Factory.StartNew(() =>
                {
                    bool exist = CompanyDao.Instance.IsExist(companyName);
                    if (exist) return -1;

                    Company company = new Company();
                    company.fullName = companyName;
                    company.shortName = companyName.Replace("有限公司", "公司").Replace("有限责任公司", "公司");
                    company.nameFirstCase = StringHelper.GetFirstPinyin(companyName);
                    company.legalPerson = "";
                    company.id = IdHelper.LongID;
                    company.isCoalSource = 0;
                    company.addUserId = App.CurrentUser?.id ?? 0;
                    company.addUserName = App.CurrentUser?.name ?? "";
                    company.updateTime = DateTime.Now;
                    company.addTime = company.updateTime;
                    company.updateUserId = company.addUserId;
                    company.updateUserName = company.addUserName;
                    company.isDelete = 0;
                    var res = CompanyDao.Instance.Sqlite.Insert(company);
                    return res;
                });

                return res;
            }
            catch (Exception ex)
            {
                var msg = "异步保存公司出错：" + ex.Message;
                Log.Error(msg);
            }
            return 0;
        }
        /// <summary>
        /// 异步保存货物 
        /// </summary>
        /// <param name="name">name</param>
        /// <returns>-1 exist; 0 failed ;1 success</returns>
        public async Task<int> SaveMaterialAsync(string name)
        {
            Debug.WriteLine($"SaveMaterialAsync:{name}");
            if (string.IsNullOrEmpty(name) || !name.Length.IsBetween(2, 30)) return 0;
            try
            {
                var res = await Task<int>.Factory.StartNew(() =>
                {
                    bool exist = MaterialDao.Instance.IsExist(name);
                    if (exist) return -1;

                    var material = new clf.weighing.entity.Material
                    {
                        categoryId = 0,
                        name = name,
                        firstCase = StringHelper.GetFirstPinyin(name),
                        affiliatedCompanyId = App.CurrentCompanyVo?.Id ?? 0,
                        id = IdHelper.LongID,
                        addTime = DateTime.Now,
                        addUserId = App.CurrentUser?.id ?? 0,
                        addUserName = App.CurrentUser?.name ?? ""
                    };
                    material.updateTime = material.addTime;
                    material.updateUserId = material.addUserId;
                    material.updateUserName = material.addUserName;
                    material.belong = (int)DataBelong.Enterprise;
                    material.isDelete = 0;
                    var res = MaterialDao.Instance.InsertMaterialAutoCreaterPrice(material);
                    return res;
                });

                return res;
            }
            catch (Exception ex)
            {
                var msg = "异步保存货物出错：" + ex.Message;
                Log.Error(msg);
            }
            return 0;
        }
    }
}
